package extended

import (
	"jvm/nzpJvm/instructions/base"
	"jvm/nzpJvm/instructions/loads"
	"jvm/nzpJvm/instructions/math"
	"jvm/nzpJvm/instructions/utils"
	"jvm/nzpJvm/runtime_area"
)

// 宽指令
type WIDE struct {
	// 宽指令对应的原始指令
	modifiedInstruction base.Instruction
}

// 宽指令获取操作数的方式不一样，要获取更多字节
func (self *WIDE) FetchOperands(reader *base.BytecodeReader) {
	opcode := reader.ReadUint8()
	switch opcode {
	case utils.OP_iload:
		inst := &loads.ILOAD{}
		// 取两个字节而不是默认的一个字节
		inst.Index = uint(reader.ReadUint16())
		self.modifiedInstruction = inst
	case utils.OP_iinc:
		inst := &math.IINC{}
		inst.Index = uint(reader.ReadUint16())
		inst.Const = int32(reader.ReadUint16())
		self.modifiedInstruction = inst
	}

}

// 调用非宽索引版本的指令的执行方法
func (self *WIDE) Execute(frame *runtime_area.Frame) {
	self.modifiedInstruction.Execute(frame)
}
